import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, HydratedDocument, Types } from 'mongoose';
import { Group } from './group.entity';
import { User } from './user.entity';
import { ApiProperty } from '@nestjs/swagger';

export type GroupUserDocument = HydratedDocument<GroupUser>;

@Schema({ collection: 'group_users' }) // 指定集合名称（当数据库表不符合命名规则时使用）
export class GroupUser extends Document {
  @ApiProperty({ description: '群ID' })
  @Prop({ type: Types.ObjectId, ref: () => Group.name })
  groupId: Types.ObjectId | Group; // 群id

  @ApiProperty({ description: '用户ID' })
  @Prop({ type: Types.ObjectId, ref: () => User.name })
  userId: Types.ObjectId | User; // 用户id

  @ApiProperty({ description: '入群时间' })
  @Prop({ type: Date, default: Date.now })
  time: Date; // 入群时间

  @ApiProperty({ description: '状态' })
  @Prop({ type: Number, default: 0 })
  state: { type: number }; // 状态（0:申请中，1:已为加入该群，2:未加入群）
}

export const GroupUserSchema = SchemaFactory.createForClass(GroupUser);
